package Common;

import java.io.IOException;

import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;

/*
 * Created on Jun 24, 2007
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Minh
 * 
 *         TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Style - Code Templates
 */
public class Sound implements Runnable {
	public static byte SND_SHOOT = 0;

	public static Thread t;
	public static boolean running;
	public static int musicVol, soundVol;

	private static Player[] player = new Player[2];
	private static Player music;
	private static int playsound = -1, playVol;
	private static Sound me;

	private static String path = "/sound";

	static {
			try {
				player[0] = Manager.createPlayer("".getClass()
						.getResourceAsStream(path + "/genderselect.mid"),
						"audio/midi"); //solidier die
				player[0].prefetch();	
//				player[1] = Manager.createPlayer("".getClass()
//						.getResourceAsStream(path + "/explo.wav"),
//						"audio/x-wav");
//				player[1].prefetch();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (MediaException e) {
				e.printStackTrace();
			}
			me = new Sound();
			running = true;
			t = new Thread(me);
			t.start();
		
	}

//	public static void play(int id, int vol) {
//		if (vol <= 0)
//			return;
//		playVol = vol;
//		playsound = id;
//	}

	public static void play(int id) {
		// playVol = 100;
		// playsound = id;
		//stopMusicPlayer(player[id]);
		try {
			if (player[id].getState() != Player.STARTED) {
				player[id].start();
				player[id].setMediaTime(0);
			}
		} catch (MediaException e) {
		}

	}

	public static void pauseMusic() {
		if (music != null) {
			if (music.getState() == Player.STARTED)
				try {
					music.stop();
				} catch (MediaException e) {
					e.printStackTrace();
				}
		}
	}

	public static void resumeMusic() {
		if (music != null) {

			try {
				if (musicVol > 0)
					music.start();
			} catch (MediaException e) {
				e.printStackTrace();
			}
		}
	}

	public static void stopMusic() {
		if (music != null) {
			if (music.getState() == Player.STARTED)
				try {
					music.stop();
				} catch (MediaException e) {
					e.printStackTrace();
				}
			music.close();
		}
	}

	public static void stopMusicPlayer(Player player) {

		if (player != null) {
			if (player.getState() == Player.STARTED)
				try {
					player.stop();
				} catch (MediaException e) {
					e.printStackTrace();
				}
				player.close();
		}
	}
	public static void playMusic(String file) {
		stopMusic();
		try {
			music = Manager.createPlayer("".getClass().getResourceAsStream(
					path + file), "audio/midi");
			
			music.setLoopCount(-1);
			
			if (musicVol > 0) {
				music.start();
				
				((VolumeControl) music.getControl("VolumeControl"))
						.setLevel(musicVol * 20);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MediaException e) {
			e.printStackTrace();
		}
	}
	
	public static void playMusicOnce(String file) {
		//stopMusic();
		try {
			music = Manager.createPlayer("".getClass().getResourceAsStream(
					path + file), "audio/midi");
			music.setLoopCount(1);
			if (musicVol > 0) {
				music.start();
				((VolumeControl) music.getControl("VolumeControl"))
						.setLevel(musicVol * 20);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MediaException e) {
			e.printStackTrace();
		}
	}

	public static void setMusicVolume(int a) {
		if (music != null && music.getState() != Player.CLOSED) {
			try {
				if (a > 0) {
					music.start();
					((VolumeControl) music.getControl("VolumeControl"))
							.setLevel(a * 20);
				} else
					music.stop();
			} catch (MediaException e) {
				e.printStackTrace();
			}
		}
		musicVol = a;
	}

	public static void setSoundVolume(int a) {
		for (int i = 0; i < player.length; i++)
			((VolumeControl) player[i].getControl("VolumeControl"))
					.setLevel(a * 20);
		soundVol = a;
	}


	public void run() {
		int i;
		while (running) {
			if (playsound > -1 && playsound < player.length) {
				i = playsound;
				playsound = -1;
				try {
					if (player[i].getState() != Player.STARTED && soundVol > 0) {
						((VolumeControl) player[i].getControl("VolumeControl"))
								.setLevel(soundVol * 20 * playVol / 100);
						// player[playsound].setMediaTime(0);
						player[i].start();
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
			}
		}
	}

}
